<?php

include './functions.php';
include './Db.php';

$data = $_POST;


if($data['password']!=$data['repassword']){
	exit(json_encode(['error'=>1,'msg'=>'两次密码不一致']));
}


$preg = "/^[a-z][a-z\d]{5,7}$/";

$c = preg_match($preg,$data['username']);

if(!$c){
	exit(json_encode(['error'=>1,'msg'=>'用户由6-8位组成，第一位不能是字母']));
}

//查用户是否存在

$db = new Db('user');
$db->where("username='{$data['username']}'");
$res = $db->select();

if($res){
	exit(json_encode(['error'=>1,'msg'=>'用户已存在']));
}

$data['create_time'] = time();

$data['password'] = md5($data['password']);

unset($data['repassword']);
unset($data['code']);

$db->save($data);
// 增加数据

exit(json_encode(['error'=>0,'msg'=>'注册成功']));
